<?php
require_once '../../include/common.inc.php';
include '../../include/PHPExcel/PHPExcel.php';
require_once '../excel.php';
//武汉登录数据
function wh_loginLog($stTime,$endTime,$logintype_sql) {
    global $db,$tablepre,$firstcount,$displaypg;
        $sql="select * from (SELECT uname ,ip ,count(*)   FROM chat_msgs where type ='3' $logintype_sql   and mtime < UNIX_TIMESTAMP('$endTime') and mtime > UNIX_TIMESTAMP('$stTime') 
     and (uname  like '%KN%'   )
    GROUP BY  uname) t2 group by ip ";

    $query=$db->query($sql);
    $resutl = [];

    $count=$db->num_rows($db->query($sql));

    while($row=$db->fetch_row($query)){
        $resutl[]=$row;
    }

    if(!empty($resutl)) {
        return [$resutl,$count];
    } else {
        return false;
    }
}

//弘奇登录数据
function hq_loginLog($stTime,$endTime,$logintype_sql) {
    global $db,$tablepre,$firstcount,$displaypg;
    $sql="select * from (SELECT uname ,ip  ,count(*)   FROM chat_msgs where type ='3' $logintype_sql  and mtime < UNIX_TIMESTAMP('$endTime') and mtime > UNIX_TIMESTAMP('$stTime') 
 and (uname  like '%KM%' )
GROUP BY  uname) t2 group by ip";

    $query=$db->query($sql);
    $count=$db->num_rows($db->query($sql));

    while($row=$db->fetch_row($query)){
        $resutl[]=$row;
    }

    if(!empty($resutl)) {
        return [$resutl,$count];
    } else {
        return false;
    }
}

function formatTime($Time)
{
    return date('m-d_H',strtotime($Time));
}

//查询武汉广州数据
function wh_hq_count($start,$end,$logintype_sql)
{
    $hq_count = hq_loginLog($start,$end,$logintype_sql);
    $wh_count = wh_loginLog($start,$end,$logintype_sql);
    return [$wh_count[1],$hq_count[1]];
}

function totalcount($start,$end,$timeword,$logintype_sql){
    $today =  date('Y-m-d',time());
    $moning_s = $today.' '.$start;
    $moning_e= $today.' '.$end;
    $count = wh_hq_count($moning_s,$moning_e,$logintype_sql);
    $total = $count[0]+$count[1];
    echo "$timeword $start - $end  总计： $total  武汉 $count[0] 广州 $count[1] </br>";

}


$logintype_sql = '';
if($logintype !== ''){
    $logintype_sql = " and logintype = $logintype " ;
}

switch($act){
    case "wh_export":
        $result = wh_loginLog($start,$end,$logintype_sql);

        $count =  $result[1];
        $result =  $result[0];

        $fileName = '武汉'.formatTime($start)."_".formatTime($end)."_".$count;

        $head_array =array('用户名','ip地址','登录次数');

        $objPHPExcel = Excel::export($result,$head_array ,$fileName,$fileName);
        $objPHPExcel->getActiveSheet()->getDefaultColumnDimension()->setWidth(15);//调节列宽

        Excel::do_export($objPHPExcel,$fileName);
        break;


    case "hq_export":

        $result = hq_loginLog($start,$end,$logintype_sql);
        $count =  $result[1];
        $result =  $result[0];

        $fileName = '弘奇'.formatTime($start)."~".formatTime($end)."~".$count;
        $head_array =array('用户名','ip地址');

        $objPHPExcel = Excel::export($result,$head_array ,$fileName,$fileName);
        $objPHPExcel->getActiveSheet()->getDefaultColumnDimension()->setWidth(15);//调节列宽
        Excel::do_export($objPHPExcel,$fileName);

        break;

        //获取固定文字报表
    case "wh_hq_export":
//        <!--                    <option value="09:00:00-12:00:00">早上</option>-->
//<!--                    <option value="13:00:00-16:00:00">下午</option>-->
//<!--                    <option value="19:00:00-22:00:00">晚上</option>-->

        totalcount('09:00:00','12:00:00','早上',$logintype_sql);
        totalcount('13:00:00','16:00:00','下午',$logintype_sql);
        totalcount('19:00:00','22:00:00','晚上',$logintype_sql);
        totalcount('09:00:00','22:00:00',date('Y年m月d日'),$logintype_sql);

//下午12：00 - 16：00 总计 285 武汉：225 广州：60
//晚上 18：00 - 22：00 总计：356  武汉 290 广州 66
       exit;

//        $fileName = '弘奇'.formatTime($start)."~".formatTime($end)."~".$count;
//        $head_array =array('用户名','ip地址');
//
//        $objPHPExcel = Excel::export($result,$head_array ,$fileName,$fileName);
//        $objPHPExcel->getActiveSheet()->getDefaultColumnDimension()->setWidth(15);//调节列宽
//        Excel::do_export($objPHPExcel,$fileName);

        break;

}
?>
<!DOCTYPE HTML>
<html>
   <head>
   <title>登录统计</title>
   <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
   <link href="../assets/css/dpl-min.css" rel="stylesheet" type="text/css" />
   <link href="../assets/css/bui-min.css" rel="stylesheet" type="text/css" />
   <link href="../assets/css/page-min.css" rel="stylesheet" type="text/css" />
   <!-- 下面的样式，仅是为了显示代码，而不应该在项目中使用-->
   <link href="../assets/css/prettify.css" rel="stylesheet" type="text/css"/>
   <style type="text/css">
code { padding: 0px 4px; color: #d14; background-color: #f7f7f9; border: 1px solid #e1e1e8; }
</style>
   </head>
   <body>
<div class="container" style="min-width:700px;">
     <ul class="breadcrumb">
<!--    <li class="active">-->
<!--         <button type="submit"  class="button button-success" id="add_group_bt"><i class="icon icon-plus icon-white"></i> 添加</button>-->
<!--         &nbsp;&nbsp;</li>-->
  </ul>
     <table  class="table table-bordered table-hover definewidth m10" >

    <form method="post" enctype="application/x-www-form-urlencoded">
        <tr>
            <td bgcolor="#FFFFFF" align="center">  <div id="group" class="bui-form-group">
                    <label>起始日期：</label>
                    <input id="start" name="start" value="" class="calendar calendar-time" type="text"><label> - </label>
                    <input id="end" value="" name="end" class="calendar calendar-time" type="text"></div>&nbsp;
            </td>
<!---->
<!--            <td class="table_logintime">今天时段：</td>-->
<!--            <td><select name="s_logintime" id="s_logintime" style="width:60px;">-->
<!--                    <option value="09:00:00-12:00:00">早上</option>-->
<!--                    <option value="13:00:00-16:00:00">下午</option>-->
<!--                    <option value="19:00:00-22:00:00">晚上</option>-->
<!--                </select>-->
<!--            </td>-->
            <td class="tableleft">是否软件登录：</td>
            <td><select name="s_logintype" id="s_logintype" style="width:60px;">
                    <option value="">全部</option>
                    <option value="1">是</option>
                    <option value="0">否</option>
                </select>
            </td>


            <td align="center" valign="middle" bgcolor="#FFFFFF"><button act="hq_export"  class="submit_btn button   button-success"  type="submit"><i class="x-icon icon-ok icon-white"></i>弘奇数据导出</button></td>
            <td align="center" valign="middle" bgcolor="#FFFFFF"><button act="wh_export"  class="submit_btn button   button-success"  type="submit"><i class="x-icon icon-ok icon-white"></i>武汉数据导出</button></td>
            <td align="center" valign="middle" bgcolor="#FFFFFF"><button act="wh_hq_export"  class="submit_btn button   button-success"  type="submit"><i class="x-icon icon-ok icon-white"></i>统计报表生成</button></td>
        </tr>

        <input type="hidden" id="act" name="act">
        <input type="hidden" id="logintype" name="logintype">

    </form>

  </table>
     <div class="row">

  </div>
   </div>
<script type="text/javascript" src="../assets/js/jquery-1.8.1.min.js"></script>
<script type="text/javascript" src="../assets/js/bui.js"></script>
<script type="text/javascript" src="../assets/js/config.js"></script>
<script type="text/javascript" src="../../upload/swfupload/swfupload.js"></script>
<script>




BUI.use('bui/overlay',function(Overlay){
            dialog = new Overlay.Dialog({
            title:'用户组权限编辑',
            width:800,
            height:600,
            buttons:[],
            bodyContent:''
          });
});
function openRule(id,type){
	dialog.set('bodyContent','<iframe src="group_rule.php?id='+id+'&type='+type+'" scrolling="yes" frameborder="0" height="100%" width="100%"></iframe>');
	dialog.updateContent();
	dialog.show();
}
function openGroupUser(id,name){
	top.topManager.openPage({
		id : 'GroupUser'+id,
		href : 'users/users.php?gid='+id,
		title : name+' 成员'
	  });
	top.topManager.reloadPage();
}

$('.submit_btn').click(function(){
    var act = $(this).attr('act');
    $("#act").val(act);
    $("#logintype").val($('#s_logintype').val());
});

$('#s_logintime').change(function(){


});

function changeTime (){
   var time =  $('#s_logintime').val();
}

  </script>
<script type="text/javascript">

    BUI.use('bui/calendar',function(Calendar){
        var datepicker = new Calendar.DatePicker({
            trigger:'.calendar-time',
            showTime:true,
            autoRender : true
        });
    });
</script>

</body>
</html>
